package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.Usersevice;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
public class ShopCartController {
    @Autowired
    private Usersevice usersevice;
    @PostMapping("/add")
    public Result addshop(@RequestBody ShoppingCartDTO cartDTO)
    {
        log.info("cartDTO is {}",cartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cartDTO,shoppingCart);
        int count = usersevice.addshopcart(shoppingCart);
        if(count==0)
        {
            return Result.error("插入失败");
        }
        else
            return Result.success();
    }
    @GetMapping("/list")
    public Result shoplist()
    {
        Long id = BaseContext.getCurrentId();
        List<ShoppingCart> shopcarts = usersevice.selectallshopcart(id);
        return Result.success(shopcarts);
    }
    @DeleteMapping("/clean")
    public Result deleteallshopcart()
    {
        usersevice.deleteall(BaseContext.getCurrentId());
        return Result.success();
    }
}
